<template>
    <div class="app-container">
        <uc-table url="admin/user/getUserPage" name="管理员" ref="adminTable" pageable table-border :search-form="searchForm">
            <template slot="searchForm">
                <el-form size="mini" :inline="true" :model="searchForm" class="demo-form-inline filter-container">
                    <el-button type="success" icon="el-icon-plus" class="filter-item" @click="handleAdd()">新 增</el-button>
                </el-form>
            </template>
            <template slot="columns">
                <el-table-column prop="userName" label="用户名" align="left" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="name" label="姓名" align="left" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="enterpriseDominantName" label="所属主体" align="left" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="email" label="联系邮箱" align="left" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="mobile" label="电话" align="left" show-overflow-tooltip ></el-table-column>
                <el-table-column prop="sex" label="性别" align="left" >
                    <template slot-scope="scope">
                        {{scope.row.sex | formDateSex}}
                    </template>
                </el-table-column>
                <el-table-column label="用户状态" prop="status">
                    <template slot-scope="scope">
                        <el-tooltip class="item" effect="dark" content="点击切换用户可用状态" placement="top">
                            <el-tag class="pointer" @click="handleChangStatus(scope.row)" :type="scope.row.status?'success':'danger'">{{scope.row.status?"可用":"禁用"}}</el-tag>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column prop="address" label="操作" width="220" align="left" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <!--编辑按钮-->
                        <el-button size="mini" icon="el-icon-edit" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button size="mini" icon="el-icon-info" type="success" @click="handleInfo(scope.row)">详情</el-button>
                        <el-button size="mini" type="danger" icon="el-icon-delete" :loading="deleteLoading" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </template>
        </uc-table>
    </div>
</template>

<script>
import UcTable from "@/components/UcTable/index.vue";
import {updateInUser,deleteInUser} from "@/api/enterprise";
export default {
    name: "adminList",
    components: {UcTable},
    data(){
        return{
            searchForm: {
                deleted:false,
                type:0,
            },
            deleteLoading:false
        }
    },
    methods:{
        handleSearch(){
            this.$refs.adminTable.getTableData(1)
        },
        handleAdd(){
            this.$router.push("/sysconfig/adminedit/0")
        },
        handleEdit(val){
            this.$router.push("/sysconfig/adminedit/"+val.ucId)
        },
        handleInfo(val){
            this.$router.push("/sysconfig/admininfo/"+val.ucId)
        },
        handleDelete(val){
            this.$confirm('您确定要执行删除'+val.name+'这个管理员吗？', '提醒信息', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                    .then(() => {
                        this.deleteLoading = true
                        deleteInUser(val).then(res=>{
                            this.deleteLoading = false
                            this.$notify.success({
                                title: '成功',
                                message: "删除管理员成功"
                            })
                            this.handleSearch()
                        }).catch(res=>{
                            this.deleteLoading = false
                            this.$notify.error({
                                title: '失败',
                                message: res.msg
                            })
                        })
                    })
                    .catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
        },
        //变更用户可用状态
        handleChangStatus(val){
            this.$confirm('您确定要执行变更' + val.name + '这个管理员状态吗？', '提醒信息', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let obj = Object.assign({},val)
                obj.status = !obj.status
                updateInUser(obj).then(res => {
                    this.$notify.success({
                        title: '成功',
                        message: "变更角色状态成功"
                    })
                    this.handleSearch()
                }).catch(res => {
                    this.$notify.error({
                        title: '失败',
                        message: res.msg
                    })
                })
            })
                    .catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消变更角色状态'
                        });
                    });
        },
    }
}
</script>

<style scoped>

</style>
